# -*- coding: utf-8 -*-
# @Time    : 2020/2/7 17:05
# @Author  : Weymouth
# @Email   : 261229090@qq.com
# @File    : 生成器.py
# @Software: PyCharm
# ---------------------------------------------------

# s = (x*2 for x in range(10))
# print(s)    #<generator object <genexpr> at 0x0000015792190C48>
# print(s.__next__())     #不建议使用
# print(next(s))  #等价于s.__next__()  in Py2:s.next()
# print(next(s))
# print(next(s))

#生成器就是一个可迭代对象（Iterable）

# for i in s:
#     print(i)



#生成器有两种创建方式
# 1.(x*2 for x in range(10))
# 2.yield
# def foo():
#     print('ok')
#     yield 1
#     print('ok2')
#     yield 2
#     return None
#
# g = foo()
# print(g)
# # next(g)
# # next(g)
# # next(g)
# for i in g:
#     print(i)


#什么是可迭代对象，(对象拥有iter方法的)
# l = [1, 2, 3]
# l.__iter__()


#
# def fib(max):
#     n, before, after = 0, 0, 1
#     while n < max:
#         print(after)
#         before, after = after, before+after
#         n = n + 1
# fib(10)

# def fib(max):
#     n, before, after = 0, 0, 1
#     while n < max:
#         # print(after)
#         yield before
#         before, after = after, before+after
#         n = n + 1
# g = fib(10)
# print(g)
# print(next(g))

def bar():
    print('ok1')
    count = yield 1
    print(count)
    print('ok2')
    yield 2
b = bar()
# next(b)
s = b.send(None)    #等同于next(b)  第一次send前如果没有next，只能传一个send(None)
# print(s)
b.send('eee')